package xyz.zq.dockerdemo.controller;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hzq
 * @date 2021/10/27 16:10
 */
@RestController
@RequestMapping("/redis")
public class RedisController {


    private final StringRedisTemplate stringRedisTemplate;

    public RedisController(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }


    @GetMapping("/set")
    public String setValueByKey(@RequestParam String key, @RequestParam String value) {
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        valueOperations.set(key, value);
        return String.format("set:{key:%s,value:%s}", key, value);
    }

    @GetMapping("/get")
    public String getValueByKey(@RequestParam String key) {
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        String value = valueOperations.get(key);
        return String.format("get:{key:%s,value:%s}", key, value);
    }
}
